package com.jelly.concurrency;

import java.util.concurrent.TimeUnit;

/**
 * 定时任务
 * 
 * @author Jelly
 * 
 */
public class LiftOff implements Runnable {

	protected int countDown = 100;
	private static int taskCount = 0;
	private final int id = taskCount++;

	public LiftOff() {
	}

	public LiftOff(int countDown) {
		this.countDown = countDown;
	}

	public String status() {
		return "#" + id + "(" + (countDown > 0 ? countDown : "Liftoff!")
				+ "). ";
	}

	@Override
	public void run() {
		synchronized (this) {
			while (countDown-- > 0) {
				System.out.println((status()));
				if(id==0)
					Thread.yield();
				System.out.println((status()));
				try {
					TimeUnit.MICROSECONDS.sleep(2);
				} catch (InterruptedException e) {
					e.printStackTrace();
				}
			}
		}
	}

	public static void main(String[] args) {
		LiftOff launch = new LiftOff();
		launch.run();
	}
}
